package src.demo;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.*;

import java.io.IOException;
import java.util.List;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2025/3/1 13:14
 */
public class MoonCompletion {

    public static void main(String[] args) throws IOException {
        try {
            String prompt = " public int bubbleSort(int[] arr){";
            long start = System.currentTimeMillis();
            OkHttpClient client = new OkHttpClient().newBuilder().build();
            MediaType mediaType = MediaType.parse("application/json");
//            RequestBody body = RequestBody.create(mediaType, "{\n  \"model\": \"moonshot-v1-8k\",\n  \"prompt\": \"public static void main, \" \n}");
            RequestBody body = RequestBody.create(mediaType, "{\n" +
                    "  \"model\": \"moonshot-v1-8k\",\n" +
                    "  \"messages\": [\n" +
                    "    {\n" +
                    "      \"role\": \"system\",\n" +
                    "      \"content\": \"你是 Kimi，由 Moonshot AI 提供的人工智能助手，你更擅长对代码进行补全。你会为用户提供安全，有帮助，准确的回答。同时，你会拒绝一切涉及恐怖主义，种族歧视，黄色暴力等问题的回答。Moonshot AI 为专有名词，不可翻译成其他语言。请只提供给用户补充部分的代码\"\n" +
                    "    },\n" +
                    "    { \"role\": \"user\", \"content\": \"" +
                    prompt +
                    "\" }\n" +
                    "  ],\n" +
                    "  \"temperature\": 0.3\n" +
                    "}");
//
            Request request = new Request.Builder()
                    .url("https://api.moonshot.cn/v1/chat/completions")
                    .method("POST", body)
//                    .addHeader("Accept", "application/json")
                    .addHeader("Content-Type","application/json")
                    .addHeader("Authorization", "sk-C8YObcyYwdnOudw7Bxc0x6CSaEpgIK4XlA5vjphT6jdFWtdP")
                    .build();
            Response response = client.newCall(request).execute();

            System.out.println(response);
//            System.out.println(response.body().string());
            System.out.println(response.body().contentType());

            // 解析 JSON 字符串
            JsonObject jsonObject = JsonParser.parseString(response.body().string()).getAsJsonObject();
            // 获取 choices 数组
            JsonArray choicesArray = jsonObject.getAsJsonArray("choices");
            if (choicesArray != null && !choicesArray.isEmpty()) {
                // 获取数组的第一个元素
                JsonObject asJsonObject = choicesArray.get(0).getAsJsonObject();
                // 提取 text 属性
                JsonObject message = asJsonObject.get("message").getAsJsonObject();
                String content = message.get("content").getAsString();
                System.out.println(content);
            }

            long end = System.currentTimeMillis();
            System.out.println(end-start);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
